package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import com.dongdongshop.service.TbContentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("tbContentCategory")
public class TbContentCategoryController {
    @Reference
    private TbContentCategoryService tbContentCategoryService ;
    @Reference
    private TbContentService tbContentService;
    @RequestMapping("toShow")
    public String toShow(){
        return "admin/content_category";
    }

    @RequestMapping("toGetInfo")
    @ResponseBody
    public Result toGetInfo(){
         List<TbContentCategory> categories =  tbContentCategoryService.selectAll();
         return Result.build(ConstantEnum.SUCCESS).setResult(categories);
    }
    //增加
    @RequestMapping("saveContentCateInfo")
    @ResponseBody
    public Result toAdd(TbContentCategory tbContentCategory){
        tbContentCategoryService.addAll(tbContentCategory);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //回显
    @ResponseBody
    @RequestMapping("toUpdate/{id}")
    public Result toUpdate(@PathVariable("id")Long id){
         TbContentCategory tbContentCategory = tbContentCategoryService.selectById(id);
         return Result.build(ConstantEnum.SUCCESS).setResult(tbContentCategory);
    }
    //修改
    @RequestMapping("updateContentCateInfo")
    @ResponseBody
    public Result toUpdate1(TbContentCategory tbContentCategory){
        tbContentCategoryService.updateAll(tbContentCategory);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //删除
    @RequestMapping("deleteBatch/{ids}")
    @ResponseBody
    public Result toBatch(@PathVariable("ids")Long[]ids){
        tbContentCategoryService.deleteBatch(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //跳转广告管理
    @RequestMapping("toShow2")
    public String toShow2(Model model) {
        List<TbContentCategory> categories = tbContentCategoryService.selectAll();
        model.addAttribute("list",categories);
        return "admin/content";
    }
    @RequestMapping("getContentInfo")
    @ResponseBody
    public Result toGetInfo2(){
         List<TbContent> tbContents = tbContentService.selectByAll();
         return Result.build(ConstantEnum.SUCCESS).setResult(tbContents);
    }
    //廣告管理增加
    @RequestMapping("addContentInfo")
    @ResponseBody
    public Result toAdd(TbContent tbContent){
        tbContentService.addAll(tbContent);
            return Result.build(ConstantEnum.SUCCESS);
    }
    //廣告回顯
    @RequestMapping("toUpdate1/{id}")
    @ResponseBody
    public Result toEcho(@PathVariable("id")Long id, Model model){
        TbContent tbContent = tbContentService.selectByID(id);
        model.addAttribute("list",tbContent);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbContent);
    }
    //修改
    @RequestMapping("updateById")
    @ResponseBody
    public Result toUpdate(TbContent tbContent){
        tbContentService.updateAll(tbContent);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //批量刪除
    @RequestMapping("deleteBatch1/{ids}")
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        tbContentService.deleteBatch(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }
}
